<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use App\Models\Good;
use Illuminate\Http\Request;
use App\Http\Controllers\Taobao\top\TopClient;
use App\Http\Controllers\Taobao\top\request\TbkItemGetRequest;

class cateController extends Controller{

    /**
     * 获得所有分类及二级分类
     */
    public function index(){
        $where = ["level"=>1,"pid"=>0,"status"=>1];
        $list = DB::table("category")->where($where)->orderBy('sort','desc')->get(['id','pid','level','img','cate_name']);
        $list = json_decode(json_encode($list), true);
        $default = "/images/default.jpg";
        $clist = [];
        if (!empty($list)){
            $cData = ["pid"=>$list[0]['id'],"status"=>1];
            $clist = DB::table("category")->where($cData)->orderBy('sort','desc')->get(['id','pid','level','img','cate_name']);
            $clist = json_decode(json_encode($clist), true);
        }
        foreach ($list as $key=>$item){
            $list[$key]['img'] = $item["img"] ? $item["img"] : $default;
            if ($key==0){
                foreach ($clist as $k=>$it){
                    $clist[$k]['img'] = $it["img"] ? $it["img"] : $default;
                }
                $list[$key]['list'] = $clist;
            }
        }
        appMessage(0,"获得顶级分类成功",$list);
    }

    /**
     * 根据分类获得二级分类列表
     */
    public function getCateList(){
        $param = request()->all();
        if (empty($param['cid'])) appMessage(1,'缺少分类信息');
        $where = ["pid"=>$param['cid'],"status"=>1];
        $list = DB::table("category")->where($where)->orderBy('sort','desc')->get(['id','pid','level','img','cate_name']);
        $list = json_decode(json_encode($list), true);
        $default = "/images/default.jpg";
        foreach ($list as $key=>$item){
            $list[$key]['img'] = $item["img"] ? $item["img"] : $default;
        }
        appMessage(0,"分类列表",$list);
    }

    /**
     * 获得商品分类列表
     * @param cid分类ID page当前页 pageSize当前页数 sort排序字段 orderBy排序方式
     */
    public function getCateProList(){
        $param = request()->all();
        if (empty($param['cid'])) appMessage(1,'未获得分类信息');
        $page = isset($param['page']) ? $param['page'] : 1;
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $limitprame = ($page -1) * $pageSize;
        $where = ["pid"=>$param['cid'],"status"=>1];
        $sort = isset($param['sort']) ? $param['sort'] : 'weigh';
        $orderBy = isset($param['orderBy']) ? $param['orderBy'] : 'desc';
        //输入分类ID获得对应的商品
        $list = DB::table("category")->where($where)->get(['id']);
        $list = json_decode(json_encode($list), true);
        $cateList = [];
        if (empty($list)){
            $cateList[] = $param['cid'];
        }else{
            $cateList = array_column($list,'id');
            $cateList[] = $param['cid'];
        }
        $taoCate = DB::table("category_relation")->whereIn('cate_id',$cateList)->get();
        $reList = json_decode(json_encode($taoCate), true);
        $taoId = [];
        if ($reList) $taoId = array_column($reList,'tao_id');
        $model = new Good();
        $res = $model->where(['shelf_status'=>1])->whereIn('cate_id',$taoId)->orderBy($sort,$orderBy)->orderBy('id', 'desc')-> skip($limitprame)->take($pageSize)-> get();;
        appMessage(0,"分类商品列表",$res);
    }

    /**
     * 获得商品详情
     * @param pid商品ID
     */
    public function proDetail(){
        $param = request()->all();
        if (empty($param['pid'])) appMessage(1,'缺少商品信息');
        $model = new Good();
        $res = $model->where('id',$param['pid'])-> first();
        if (empty($res)) appMessage(1,'未找到商品信息');
        if (empty($res['shelf_status'])) appMessage(1,'该商品还未上架');
        appMessage(0,"商品详情",$res);
    }
}